package Servlet;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import DAO.DatabaseDAO;
import Flights.Flight;
import Flights.Flights;

/**
 * Servlet implementation class AddFlightServlet
 */
@WebServlet("/AddFlightServlet")
public class AddFlightServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
       
    /**
     * @see HttpServlet#HttpServlet()
     */
    public AddFlightServlet() {
        super();
    }

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		ServletContext ctx = getServletContext();
		HttpSession session = request.getSession();
		
		PrintWriter out = response.getWriter();
		
		String client = request.getParameter("client");
		if(client==null){
			client="web";
		}
		
		String flightString = "";
		
		Date datumPolaska = null;
		Date datumDolaska = null;
		
		if(client.equals("web")){
			try {
				datumPolaska = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").parse(request.getParameter("datumPolaska"));
				datumDolaska = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm").parse(request.getParameter("datumDolaska"));
			} catch (ParseException e) {
				System.out.println("Neispravan datum");
			}
		} else {
			try {
				datumPolaska = new SimpleDateFormat("dd.MM.yyyy_HH:mm").parse(request.getParameter("datumPolaska"));
				datumDolaska = new SimpleDateFormat("dd.MM.yyyy_HH:mm").parse(request.getParameter("datumDolaska"));
			} catch (ParseException e) {
				System.out.println("Neispravan datum");
			}
		}
		
		Flight flight = new Flight(request.getParameter("idLeta"),
				datumPolaska,
				datumDolaska,
				request.getParameter("polazniAerodrom"),
				request.getParameter("odrediste"),
				Integer.parseInt(request.getParameter("brojPutnika")));

		Flights flights = (Flights) session.getAttribute("flights");
		if(flights==null){
			flights = new Flights(ctx.getRealPath("") + "/listaLetova/flights.txt");
		}
		flights.setFlight(flight);
		
		flightString = request.getParameter("idLeta") + ";" + 
						new SimpleDateFormat("dd. MMMM yyyy, HH:mm").format(datumPolaska) + ";" + 
						new SimpleDateFormat("dd. MMMM yyyy, HH:mm").format(datumDolaska) + ";" + 
						request.getParameter("polazniAerodrom") + ";" + 
						request.getParameter("odrediste") + ";" + 
						request.getParameter("brojPutnika");
		
//		System.out.println(flightString);

		DatabaseDAO dao = DatabaseDAO.getInstance();
		dao.save(flightString, ctx.getRealPath("")+"/listaLetova/flights.txt");
		
		File let = new File(ctx.getRealPath("")+"/listaLetova/" + request.getParameter("idLeta") + ".txt");
		if(!let.exists()) {
		    let.createNewFile();
		}
		
		if(client.equals("CommandPrompt")){
			out.println("Uspesno ste dodali let"); 
			out.close();
		} else {
			RequestDispatcher disp = getServletContext().getRequestDispatcher("/let_dodat.faces");
			disp.forward(request, response);
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		doGet(request, response);
	}

}
